const db = require('./db.js')

exports.getAllCates = async () => {
    try {
        const sqlStr = `select id,title from cms_img_category order by sort desc`
        return await db.query(sqlStr)
    } catch (err) {
        throw err
    }
}

exports.getImgsByCateId = async (cateid) => {
    try {
        let sqlStr = `select id,title,img_url,zhaiyao from cms_imgs `
        if (cateid != 0) {
            sqlStr += ` where cate_id=${cateid} `
        }
        sqlStr += ' order by sort desc,id desc '
        return await db.query(sqlStr)
    } catch (err) {
        throw err
    }
}

exports.getImgById = async (id) => {
    try {
        const sqlStr = `select id,title,click,add_time,content from cms_imgs where id=${id} `
        const [img] = await db.query(sqlStr)
        return img
    } catch (err) {
        throw err
    }
}

exports.getImgThumbs = async (imgid) => {
    try {
        const sqlStr = `select id,src from cms_img_thumbs where img_id=${imgid} order by sort desc,id desc`
        return await db.query(sqlStr)   
    } catch(err) {
        throw err
    }
}